﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCT.Entities;
using DCT.Data;

public partial class Admin_DanhMuc_DMLoaiTinTuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Bind_Data();
        }
    }
    protected void Bind_Data()
    {
        try
        {
            DCT.Entities.TList<DCT.Entities.LoaiTinTuc> TList_LoaiTinTuc = DataRepository.LoaiTinTucProvider.GetAll();
            gridCatalog.DataSource = TList_LoaiTinTuc;
            gridCatalog.DataBind();
        }
        catch (Exception)
        {

        }
    }


    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        using (DCT.Entities.LoaiTinTuc entLoaiTinTuc = new DCT.Entities.LoaiTinTuc())
        {
            entLoaiTinTuc.Id = Convert.ToInt32(H_CatalogId.Value);
            entLoaiTinTuc.TenLoaiTinTuc = Common.StandardString(txtTen.Text);
            bool updateStatus = false;
            try
            {
                if (entLoaiTinTuc.Id == 0)
                {
                    updateStatus = DataRepository.LoaiTinTucProvider.Insert(entLoaiTinTuc);
                }
                else
                {
                    entLoaiTinTuc.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.LoaiTinTucProvider.Update(entLoaiTinTuc);
                }
                if (!updateStatus)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LOAITINTUC_CAPNHAT_THATBAI, true);
                }
                else
                {
                    H_CatalogId.Value = "0";
                    txtTen.Text = "";
                    Bind_Data();
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.LOAITINTUC_CAPNHAT_THANHCONG, true);

                }
            }
            catch (Exception)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LOAITINTUC_CAPNHAT_THATBAI, true);

            }
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        txtTen.Text = "";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand'; this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='White';";
            LinkButton l = (LinkButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?')");

        }
    }
    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Bind_Data();
        txtTen.Text = "";
        H_CatalogId.Value = "0";
    }
    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            try
            {
                int Id = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.LoaiTinTucProvider.Delete(Id);
                if (!deleteStatus)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LOAITINTUC_XOA_THATBAI, true);

                }
                else
                {
                    Bind_Data();
                    H_CatalogId.Value = "0";
                    txtTen.Text = "";
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.LOAITINTUC_XOA_THANHCONG, true);
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMLoaiTinTuc.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LOAITINTUC_XOA_THATBAI, true);
                return;
            }
        }
        if (e.CommandName == "Select")
        {
            try
            {
                int Id = Convert.ToInt32(e.CommandArgument.ToString());
                using (LoaiTinTuc entLoaiTinTuc = DataRepository.LoaiTinTucProvider.GetById(Id))
                {
                    if (entLoaiTinTuc != null)
                    {
                        H_CatalogId.Value = entLoaiTinTuc.Id.ToString();
                        txtTen.Text = entLoaiTinTuc.TenLoaiTinTuc;
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}